#define DEBUG
#include<cstdio>
#include <cstring>
#include <algorithm>

namespace IO{
	int c;
	unsigned int seed;
	unsigned int randnum(){
		seed^=seed<<13;
		seed^=seed>>17;
		seed^=seed<<5;
		return seed;
	}

	inline int read(int &x){scanf("%d",&x);return x;}
	inline void init_case(int &m,int &a,int &b,int &d,int p[]){
		scanf("%d%u%d%d%d%d",&m,&seed,&a,&b,&c,&d);
		for(int i=1;i<=m;i++){
			if(randnum()%c==0)p[i]=-1;
			else p[i]=randnum()%b;
		}
	}

	inline void update_ans(unsigned int &ans_sum,unsigned int cur_ans,int no){
		const static unsigned int mod=998244353;
		ans_sum^=(long long)no*(no+7)%mod*cur_ans%mod;
	}
}

using IO::read;
using IO::init_case;
using IO::update_ans;
using namespace std;

typedef unsigned int uint;
const int maxm=1000000, maxb=2*maxm;

int sta[maxb+1], flyq[maxm+1], que[maxm+1], fh, ft, qh, qt;
//Sta : 0=No Appearing; 1=On the track; 2=Flying away;

void push(int k) {
    for (; qt>=qh && que[qt]>k; qt--);
    que[++qt] = k;
}

void pop(int k) {
    if (que[qh]==k) {
        qh++;
    }
}

int main() {
    freopen("sage.in", "r", stdin);
    freopen("sage.out", "w", stdout);

    static int p[maxm+1];
    int t, m, a, b, d, nowm;
    read(t);
    for (; t--; ) {
        uint ans=0;
        init_case(m, a, b, d, p);
        memset(sta, 0, sizeof sta);
        fh=qh=1, ft=qt=0;
        for (int i=0; i<=a; i++) {
            sta[i] = 1;
        }
        nowm = a+1;
        for (int i=1; i<=m; i++) {
            int x=p[i];
            if (p[i]==-1 || sta[x]==2) {
                if (fh>ft || d) continue;
                int x=flyq[fh++];
                sta[x] = 1;
                pop(x);
            } else if (!sta[x]) {
                sta[x] = 1;
            } else if (sta[x]==1) {
                if (d) continue;
                sta[x] = 2;
                flyq[++ft] = x;
                push(x);
            }
            for (; sta[nowm]; nowm++);
            update_ans(ans, min(nowm, qh<=qt ? que[qh] : nowm), i);
        }
        printf("%u\n", ans);
    }

    fclose(stdin);
    fclose(stdout);
    return 0;
}
